// const secret = require('./secret')
const nconf = require('nconf')
const path = require('path')

module.exports = app => {
  nconf.use('file', {
    file: path.join(app.config.baseDir, 'app/mock/db.json')
  })

  class Model {
    constructor(ctx) {
      this.ctx = ctx
    }

    async getClient(clientId, clientSecret) {
      const client = nconf.get('client')
      if (clientId !== client.clientId || clientSecret !== client.clientSecret) {
        return
      }
      return client
    }

    async getUser(username, password) {
      const user = nconf.get('user')
      if (username !== user.username || password !== user.password) {
        return
      }
      return { userId: user.id }
    }

    async saveToken(token, client, user) {
      const _token = Object.assign({}, token, { user }, { client })
      nconf.set('token', _token)
      nconf.save()
      return _token
    }

    async getAccessToken(bearerToken) {
      const token = nconf.get('token')
      token.accessTokenExpiresAt = new Date(token.accessTokenExpiresAt)
      token.refreshTokenExpiresAt = new Date(token.refreshTokenExpiresAt)
      const user = nconf.get('user')
      const client = nconf.get('client')
      token.user = user
      token.client = client
      return token
    }

    async saveAuthorizationCode(code, client, user) {
      console.log('-----')
      console.log(client.id)
      console.log('-----')
      try {
        return {
          authorizationCode: code.authorizationCode,
          expiresAt: code.expiresAt,
          redirectUri: code.redirectUri,
          scope: code.scope,
          client: { id: client.id },
          user: { id: user.id }
        }
      } catch (err) {
        return false
      }
    }

    async getAuthorizationCode(authorizationCode) {
      console.log('-----')
      console.log('getAuthorizationCode', authorizationCode)
      console.log('-----')
      try {
        console.log('authorizationCode: ', authorizationCode)
        return {
          code: authCode.code,
          expiresAt: authCode.expiresAt,
          redirectUri: authCode.redirectUri,
          scope: authCode.scope,
          client: { id: authCode.clientId },
          user: user
        }
      } catch (err) {
        return false
      }
    }
  }

  return Model
}

/*
module.exports = () => {
  const model = {}
  // getClient –> grantTypeAllowed –> getUser –> saveAccessToken
  model.getClient = async (clientId, clientSecret, callback) => {
    if (clientId === secret.clientId && clientSecret === secret.clientSecret) {
      callback(null, { clientId, clientSecret })
      return
    }
    callback(null, null)
  }

  model.grantTypeAllowed = async (clientId, grantType, callback) => {
    let allowed = false
    if (grantType === 'password' && clientId === secret.clientSecret) {
      allowed = true
    }
    callback(null, allowed)
  }

  model.getUser = async (username, password, callback) => {

  }

  //

  model.saveAccessToken = (accessToken, clientId, expires, user, callback) => {}

  model.getAccessToken = (bearerToken, callback) => {}

  return model
}
*/
